#include "common.h"
#include "fundamental.h"
#include "object.h"

#include "actormanager.h"
#include "actor.h"

IMPLEMENT_RTTI( Actor, Fundamental::Object )

Actor::Actor
(
	const std::string&		type, 
	const std::string&		name,
	ActorTypeInfo&			desc
)
:	Fundamental::Object( name )
,	_desc( desc )
,	_isRenderable( false )
,	_pParams( NULL )
,	_type( type )
{
	D3DXMatrixIdentity( &_matObject );
}

Actor::~Actor()
{
	if( _pParams )
	{
		delete _pParams;
		_pParams = NULL;
	}
}

Actor* Actor::Create( const std::string& type, const std::string& name )
{
	ActorManager::TypeList& types = ActorManager::_types;
	ActorManager::TypeListIter it = types.find( type );
	if( it == types.end() )
	{
		assert( false && "Register class first!" );
		return NULL;
	}

	Actor* pActor = new Actor( type, name, *( it->second ) );
	pActor->_pParams = new lua_tinker::table( pActor->_desc.L );

	return pActor;
}

D3DXVECTOR3 Actor::GetPosition()
{
	return D3DXVECTOR3( _matObject._41, _matObject._42, _matObject._43 );
}

void Actor::Initialize()
{
	lua_tinker::call< void >( _desc.L, "Initialize", *_pParams );
}

void Actor::SetPosition( const D3DXVECTOR3& pos )
{
	_matObject._41 = pos.x;
	_matObject._42 = pos.y;
	_matObject._43 = pos.z;
}

void Actor::Update( float deltaTime )
{
	lua_tinker::call< void >( _desc.L, "Update", *_pParams, deltaTime );
}